package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.domain.PropertyValue;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.service.IPropertyValuesService;
import cn.wolfcode.shop.vo.JSONResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性相关
 */
@Controller
@RequestMapping("propertys")
public class PropertyController {
    @Reference
    private ICatalogService catalogService;
    @Reference
    private IPropertyService propertyService;
    @Reference
    private IPropertyValuesService propertyValuesService;
    @RequestMapping("view")
    public String view(Model model){
        //查询所有商品分类
        List<Catalog> catalogs = catalogService.listAll();
        String allCatalog = JSON.toJSONString(catalogs);
        model.addAttribute("allCatalog",allCatalog);
        return "property/property";
    }

    @PostMapping
    @ResponseBody
    public JSONResult saveOrUpdate(Property property){
        JSONResult jsonResult=new JSONResult();
        propertyService.saveOrUpdate(property);
        return jsonResult;
    }

    @DeleteMapping("{id}")
    @ResponseBody
    public JSONResult deleteById(@PathVariable("id") Long id){
        JSONResult jsonResult=new JSONResult();
        propertyService.deleteById(id);
        return jsonResult;
    }

    /**
     * 获取指定属性的属性值信息
     * @return 返回模板
     */
    @GetMapping("{propertyId}/propertyValues")
    public String getPropertyValues(Model model,@PathVariable("propertyId") Long propertyId){
        List<PropertyValue> propertyValues=propertyValuesService.getValuesByPropertyId(propertyId);
        model.addAttribute("propertyValueList",propertyValues);
        return "property/property_value_list";
    }

}
